package com.googlecode.hypersphere.ws2ejb3.oaw;

/*import j2ee.CmpFieldType;
import j2ee.EntityType;
*/

import j2ee.CmpFieldType;
import j2ee.EjbRelationshipRoleType;
import j2ee.EntityType;

import java.io.File;

import com.googlecode.hypersphere.ws2ejb3.xml.Field;
import com.googlecode.hypersphere.ws2ejb3.xml.MapxmiParser;
import com.googlecode.hypersphere.ws2ejb3.xml.Relationship;
import com.googlecode.hypersphere.ws2ejb3.xml.XPathQuery;

public class EjbNavigator {

	private static MapxmiParser createParser(String path) {
		return new MapxmiParser(new File(path),
				"META-INF/backends/ORACLE_V9_1/Map.mapxmi", new XPathQuery());
	}
	
	public static String getEntityTableName(String path, EntityType entity) {
		MapxmiParser parser = createParser(path);
		return parser.getEntityTableName(entity.getEjbName());
	}
	
	public static boolean isPrimaryKey(String path, CmpFieldType cmpField) {
		Field field = EjbNavigator.getField(path, cmpField);
		return field.isPrimaryKey();
	}
	
	public static String getFieldType(String path, CmpFieldType cmpField) {
		Field field = EjbNavigator.getField(path, cmpField);
		return field.getType();
	}
	
	public static Field getField(String path, CmpFieldType cmpField) {
		MapxmiParser parser = createParser(path);
		String id = (String)cmpField.getId();
		return parser.getField(id, cmpField.getFieldName());
	}
	
	public static boolean equals(String string1, String string2) {
		return string2.equals(string1);
	}
	
	private static Relationship getRelationship(String path, EjbRelationshipRoleType ejbRelationshipRoleType) {
		MapxmiParser parser = createParser(path);
		return parser.getRelationship((String)ejbRelationshipRoleType.getId());
	}
	
	public static String getRelationTableName(String path, EjbRelationshipRoleType ejbRelationshipRoleType) {
		return EjbNavigator.getRelationship(path, ejbRelationshipRoleType).getTableName();
	}
	
	public static String getJoinColumn(String path, EjbRelationshipRoleType ejbRelationshipRoleType) {
		return EjbNavigator.getRelationship(path, ejbRelationshipRoleType).getJoinColumn();
	}
	
	public static String getFieldTableName(String path, CmpFieldType cmpField) {
		Field field = EjbNavigator.getField(path, cmpField);
		return field.getLink().getId();
	}

}
